<?php
/**
 *  Create On 2014-11-11
 *  Author Yiwei
 *  QQ:1006629314
 **/
class TRANS{

	protected $_head = null;
	protected $_nodeTree = null;
	protected $_head_id = null;
	protected $_head_subject = null;
	protected $_head_type = null;
	protected $_join = null;
	
	protected $_targetDb = null;
	
	public function __construct($data,$join,$targetDb){
		echo 'trans oldId:'.$data['id'].'->';
		$this->_targetDb = $targetDb;
		
		$this->_head_subject = $data['sub_id'];
		$this->_head_type = $data['type_id'];
		$json = json_decode($data['json'],true);
		$this->_head = $json['head'];
		$this->_join = $join;
		$this->_nodeTree = $json['nodeTree'];
	}
	
	public function trans(){
		$headIndex = $this->_transHead();
		if($headIndex){echo 'trans head succ-head_id:'.$headIndex.'->';}
		else{ echo 'trans head error'; exit;}
		$this->_head_id = $headIndex;
		$result = $this->_transTree($this->_nodeTree,$headIndex,0);
		if($result){echo 'trans nodeTree succ->';}
		else{ echo 'trans nodeTree error';exit;}
	}
	
	private function _transHead(){
		$recordId = $this->_insertSource();
		if(!$recordId) { echo 'trans source fail'; exit;}
		return $this->_insertHead($recordId,$this->_head_subject,$this->_head_type,
			$this->_join['content'],$this->_join['answer'],$this->_join['parse']);
	}
	
	private function _transTree($node,$item_id,$pindex){
		$pid = $this->_insertNode($node,$item_id, $pindex);
		//var_dump($pid);
		
		foreach ($node['knows'] as $val){
			if(!$this->_insertKnows($val, $pid))
			{echo 'trans knows fail';exit;}
		}
		foreach ($node['skill'] as $val){
			if(!$this->_insertSkill($val, $pid))
			{echo 'trans skill fail';exit;}
		}
		$children = $node['children'];
		for($k = 0; $k <count($children); $k++){
			$result = $this->_transTree($children[$k],$item_id,$pid);
			if(!$result){echo 'trans node'.$children[$k]['index'].' error';exit;}
		}
		return $pid;
	}
	
	private function _insertSource(){
		$data = $this->_head['examSource']['examSourceControls'];
		$sql = 'INSERT INTO `xdf_exam_source_record` 
				(provinces,city,school,academic_year,semester,
				education_type,grade,exam_branch,month)
				VALUES (
				'.$data['fourLevelControls']['province'].',
				'.$data['fourLevelControls']['city'].',
				'.$data['fourLevelControls']['school'].',
				'.$data['otherControls']['academic_year'].',
				'.$data['otherControls']['semester'].',	
				'.$data['otherControls']['education_type'].',
				'.$data['otherControls']['grade'].',
				'.$data['otherControls']['exam_branch'].',
				'.$data['otherControls']['month'].'
				)';
		return $this->_targetDb->insert($sql);
	}
	
	private function _insertHead($recordId,$subject,$type,$content,$answer,$parse){
		$sql = 'INSERT INTO `xdf_item` (item_source_id,sub_id,itemtype_id,
							content,answer,parse,create_time)
				VALUES ('.$recordId.','.$subject.','.$type.',
						"'.addslashes($content).'",
						"'.addslashes($answer).'",
						"'.addslashes($parse).'",NOW())';
		//echo $sql;
		return $this->_targetDb->insert($sql);
	}
	
	private function _insertNode($node,$item_id,$pindex){
		$sql = 'INSERT INTO `xdf_item_node` (item_id,pid,type,content,answer,parse)
				VALUES ('.$item_id.','.$pindex.','.$node['type'].',
						"'.addslashes($node['content']).'",
						"'.addslashes($node['answer']).'",
						"'.addslashes($node['parse']).'")';
		//echo $sql;
		return $this->_targetDb->insert($sql);
	}
	
	private function _insertKnows($knows,$pid){
		$sql = 'INSERT INTO `xdf_node_knowledge` (item_id,node_id,knowledge_id,create_time)
				VALUES ('.$this->_head_id.','.$pid.','.$knows['id'].',NOW())';
		return $this->_targetDb->insert($sql);
	}

	private function _insertSkill($skill,$pid){
		$sql = 'INSERT INTO `xdf_node_skill` (item_id,node_id,skill_id,create_time)
				VALUES ('.$this->_head_id.','.$pid.','.$skill['id'].',NOW())';
		return $this->_targetDb->insert($sql);
	}
	
}